REBOL [title: "save bean-templates" file: %save-bean-templates.r]

do %rep-mezz.r
do %index-xml-2.r
do %bean-funcs.r

set-modes system/ports/input [binary: true]
data: load-xml as-string copy system/ports/input
;?? data
result-template: copy/deep data
data: data/b/java/b/array/b
templates: copy []
inbuild-to-val: copy []
own-to-val: copy []
val-to-bean: copy []
forskip data 6 [
    set [x x desc-body x x template-body] data
    unless desc-body [break]
    desc: load de-xml desc-body/b/string/b/1
    template: template-body/b/3
    repend templates [desc/1 template]
    repend val-to-bean either paren? desc/3 [
    	[desc/1 compose[target: (desc/2) . (to-block desc/3)]]
    ][
        [desc/1 compose [(to-set-path desc/2) (desc/3)]]
    ]
            setter: either setter: select desc 'to-reb[
            	compose[val: (desc/2) . (setter)]
            ][
            	reduce ['to desc/1 desc/2]
            ]
    
    either "object" = template/1 [
        obj: reduce [desc/1 desc/2]
        repend own-to-val [
            template/a/class
            setter 
        ]
    ] [
        repend inbuild-to-val [
            template/1 setter
        ]
    ]
]

new-line/skip own-to-val true 2
new-line/skip val-to-bean true 2

append inbuild-to-val compose/deep/only [
    "object" [
        switch/default bean-tag/a/class (own-to-val) [make error! join "unknown own bean: " bean-tag/a/class]
    ]
]

ta: top-array result-template
te: copy/part ta/b 3
clear ta/b 
clear te/void/b
src: compose/only/deep [
	bean-result-entry: (te)
	bean-result-template: (result-template/b)
    bean-templates: (templates)
    mark-record: func[blk ofs][
	new-line skip tail blk negate ofs true
]
    
    bean-to-val: func [bean-tag /local val] [
        switch/default bean-tag/1 (inbuild-to-val) [make error! join "unknown inbuild bean: " bean-tag/1]
    ]
    val-to-bean: func [val /local type bean-tag target] [
        type: type?/word :val ; lit-word needs :val
        bean-tag: copy/deep select bean-templates type
        switch/default type (val-to-bean) [
            make error! join "Unbekannter Typ: " remold [
                type? val
                val
            ]
        ]
        reduce [
            to-word bean-tag/1
            bean-tag
        ]
     ]
]
append src load %bean-funcs.r
;?? src
save/all/header %bean-templates.r src [Title: "Templates  für bean-xml"]